package study.java.jdk.jdk8._1_stream.sort;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

@Slf4j
public class Sort_Test {

    Map<String, Integer> sortRule = MapUtil.ofEntries(
            MapUtil.entry("语文", 1),
            MapUtil.entry("数学", 2),
            MapUtil.entry("英语", 3),
            MapUtil.entry("物理", 4),
            MapUtil.entry("化学", 5),
            MapUtil.entry("生物", 6),
            MapUtil.entry("政治", 7),
            MapUtil.entry("历史", 8),
            MapUtil.entry("地理", 9)
    );

    @Test
    public void test1() {
        List<SubjectScore> dataLt = list();
        log.info(">>>>>> {}", dataLt);

        // 排序
        Comparator<SubjectScore> otherComparator = Comparator.comparing(SubjectScore::getPapId);
        Comparator<SubjectScore> comparator = Comparator.comparing(e -> MapUtil.getInt(sortRule, e, 100));
        comparator.thenComparing(otherComparator);
        Collections.sort(dataLt, comparator);
        log.info(">>>>>> {}", dataLt);
    }

    @Builder
    @Data
    private static class SubjectScore {
        private Long papId;
        private String subjectName;
        private BigDecimal score;
    }

    List<SubjectScore> list() {
        return ListUtil.toList(
                SubjectScore.builder().subjectName("语文").papId(1003L).build(),
                SubjectScore.builder().subjectName("语文").papId(1002L).build(),
                SubjectScore.builder().subjectName("语文").papId(1001L).build(),
                SubjectScore.builder().subjectName("语文").papId(1001L).build());
    }


    @Test
    public void rename_test() {
        List<SubjectScore> list = list();
        Map<String, Integer> seqMap = Maps.newHashMap();
        for (SubjectScore score : list) {
            //
            String name = score.getSubjectName();
            if (seqMap.containsKey(name)) {
                // 计算最新 seq
                Integer curSeq = MapUtil.getInt(seqMap, name);
                // 修改
                score.setSubjectName(StrUtil.format("{}{}", name, curSeq));
                // 保存最新
                seqMap.put(name, ++curSeq);
            } else {
                seqMap.put(name, 1); //序号
            }
        }
        log.info(">>>>>> {}", list);

    }
}
